/**
 * 数字工具类
 */
import objUtil from './obj-util'

// 数字格式化
function format(num, decimal = 2) {
  if (num === undefined || num === null || num === '') {
    return ''
  }
  if (typeof num === 'string') {
    num = Number(num)
  }
  if (typeof num !== 'number') {
    return ''
  }
  return num.toFixed(decimal)
}

// 数字格式化
function formatNum(num, decimal = 2) {
  if (num === undefined || num === null || num === '') {
    return ''
  }
  if (typeof num === 'string') {
    num = Number(num)
  }
  if (typeof num !== 'number') {
    return ''
  }
  const str = num.toFixed(decimal)
  let integer = str
  let decimals = ''
  if (str.includes('.')) {
    [integer, decimals] = str.split('.')
  }
  const reg = /\d{1,3}(?=(\d{3})+$)/g
  integer = integer.replace(reg, '$&,')
  return decimals ? `${integer}.${decimals}` : integer
}

// 数字格式化
function formatNumObj(num, decimal = 2) {
  if (num === undefined || num === null || num === '') {
    return {
      integer: '',
      decimal: ''
    }
  }
  if (typeof num === 'string') {
    num = Number(num)
  }
  if (typeof num !== 'number') {
    return {
      integer: '',
      decimal: ''
    }
  }
  const str = num.toFixed(decimal)
  let integer = str
  let decimals = ''
  if (str.includes('.')) {
    [integer, decimals] = str.split('.')
  }
  const reg = /\d{1,3}(?=(\d{3})+$)/g
  integer = integer.replace(reg, '$&,')
  return {
    integer,
    decimal: decimals
  }
}

// 数字格式化
function formatNumArr(num, decimal = 2) {
  if (num === undefined || num === null || num === '') {
    return ['', '']
  }
  if (typeof num === 'string') {
    num = Number(num)
  }
  if (typeof num !== 'number') {
    return ['', '']
  }
  const str = num.toFixed(decimal)
  let integer = str
  let decimals = ''
  if (str.includes('.')) {
    [integer, decimals] = str.split('.')
  }
  const reg = /\d{1,3}(?=(\d{3})+$)/g
  integer = integer.replace(reg, '$&,')
  return [integer, decimals]
}

// 数字格式化
function formatNumStr(num, decimal = 2) {
  if (num === undefined || num === null || num === '') {
    return ''
  }
  if (typeof num === 'string') {
    num = Number(num)
  }
  if (typeof num !== 'number') {
    return ''
  }
  const str = num.toFixed(decimal)
  let integer = str
  let decimals = ''
  if (str.includes('.')) {
    [integer, decimals] = str.split('.')
  }
  const reg = /\d{1,3}(?=(\d{3})+$)/g
  integer = integer.replace(reg, '$&,')
  return decimals ? `${integer}元${decimals}分` : `${integer}元整`
}

// 数字格式化
function formatNumChinese(num, decimal = 2) {
  if (num === undefined || num === null || num === '') {
    return ''
  }
  if (typeof num === 'string') {
    num = Number(num)
  }
  if (typeof num !== 'number') {
    return ''
  }
  const str = num.toFixed(decimal)
  let integer = str
  let decimals = ''
  if (str.includes('.')) {
    [integer, decimals] = str.split('.')
  }
  const reg = /\d{1,3}(?=(\d{3})+$)/g
  integer = integer.replace(reg, '$&,')
  const units = ['', '万', '亿', '万亿']
  const nums = integer.split(',').reverse()
  let res = ''
  for (let i = 0; i < nums.length; i++) {
    res = nums[i] + units[i] + res
  }
  return decimals ? `${res}.${decimals}` : res
}

// 数字格式化
function formatNumChineseStr(num, decimal = 2) {
  if (num === undefined || num === null || num === '') {
    return ''
  }
  if (typeof num === 'string') {
    num = Number(num)
  }
  if (typeof num !== 'number') {
    return ''
  }
  const str = num.toFixed(decimal)
  let integer = str
  let decimals = ''
  if (str.includes('.')) {
    [integer, decimals] = str.split('.')
  }
  const reg = /\d{1,3}(?=(\d{3})+$)/g
  integer = integer.replace(reg, '$&,')
  const units = ['', '万', '亿', '万亿']
  const nums = integer.split(',').reverse()
  let res = ''
  for (let i = 0; i < nums.length; i++) {
    res = nums[i] + units[i] + res
  }
  return decimals ? `${res}点${decimals}` : res
}

// 数字格式化
function formatNumUnit(num, decimal = 2) {
  if (num === undefined || num === null || num === '') {
    return ''
  }
  if (typeof num === 'string') {
    num = Number(num)
  }
  if (typeof num !== 'number') {
    return ''
  }
  const units = ['', 'K', 'M', 'G', 'T', 'P']
  let index = 0
  while (num >= 1000 && index < units.length - 1) {
    num /= 1000
    index++
  }
  return `${num.toFixed(decimal)}${units[index]}`
}

// 数字格式化
function formatNumUnitObj(num, decimal = 2) {
  if (num === undefined || num === null || num === '') {
    return {
      num: '',
      unit: ''
    }
  }
  if (typeof num === 'string') {
    num = Number(num)
  }
  if (typeof num !== 'number') {
    return {
      num: '',
      unit: ''
    }
  }
  const units = ['', 'K', 'M', 'G', 'T', 'P']
  let index = 0
  while (num >= 1000 && index < units.length - 1) {
    num /= 1000
    index++
  }
  return {
    num: num.toFixed(decimal),
    unit: units[index]
  }
}

// 数字格式化
function formatNumUnitArr(num, decimal = 2) {
  if (num === undefined || num === null || num === '') {
    return ['', '']
  }
  if (typeof num === 'string') {
    num = Number(num)
  }
  if (typeof num !== 'number') {
    return ['', '']
  }
  const units = ['', 'K', 'M', 'G', 'T', 'P']
  let index = 0
  while (num >= 1000 && index < units.length - 1) {
    num /= 1000
    index++
  }
  return [num.toFixed(decimal), units[index]]
}

// 数字格式化
function formatNumUnitStr(num, decimal = 2) {
  if (num === undefined || num === null || num === '') {
    return ''
  }
  if (typeof num === 'string') {
    num = Number(num)
  }
  if (typeof num !== 'number') {
    return ''
  }
  const units = ['', '千', '百万', '十亿', '万亿']
  let index = 0
  while (num >= 1000 && index < units.length - 1) {
    num /= 1000
    index++
  }
  return `${num.toFixed(decimal)}${units[index]}`
}

export default {
  format,
  formatNum,
  formatNumObj,
  formatNumArr,
  formatNumStr,
  formatNumChinese,
  formatNumChineseStr,
  formatNumUnit,
  formatNumUnitObj,
  formatNumUnitArr,
  formatNumUnitStr
}
